home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / mus / play / tracker_4_31.lzh / tracker / Arch / common.c next >
C/C++ Source or Header  |  1995-05-12  |  8KB  |  419 lines

  1. /* common.c */
  2.  
  3. /* common repository of code/macros for all architectures audio */
  4. /* $Id: common.c,v 1.9 1995/05/12 20:40:33 espie Exp espie $ */
  5. /* $Log: common.c,v $
  6.  * Revision 1.9  1995/05/12  20:40:33  espie
  7.  * Added audio_ui capability.
  8.  *
  9.  * Revision 1.8  1995/05/12  13:52:19  espie
  10.  * New synchronization for sparc.
  11.  * News ulaw conversion function.
  12.  *
  13.  * Revision 1.7  1995/03/17  00:30:59  espie
  14.  * Fixed multiple stupid bugs in common.c (thanks Rolf)
  15.  *
  16.  * Revision 1.6  1995/02/27  14:25:37  espie
  17.  * Rolf Grossmann patch.
  18.  *
  19.  * Revision 1.5  1995/02/26  23:07:14  espie
  20.  * Changed sync to tsync.
  21.  *
  22.  * Revision 1.4  1995/02/23  23:33:01  espie
  23.  * Linear resampling changed.
  24.  *
  25.  * Revision 1.3  1995/02/23  22:41:45  espie
  26.  * Added # of bits.
  27.  *
  28.  * Revision 1.2  1995/02/23  17:03:14  espie
  29.  * Continuing changes for a standard file.
  30.  *
  31.  * Revision 1.1  1995/02/23  16:42:10  espie
  32.  * Initial revision
  33.  *
  34.  *
  35.  */
  36.  
  37. #define abs(x) ((x) < 0 ? -(x) : (x))
  38.  
  39. /* f' = best_frequency(f, table, def):
  40.  * return nearest frequency in sorted table,
  41.  * unless f == 0 -> return def
  42.  */
  43. LOCAL int best_frequency(f, table, def)
  44. int f;
  45. int table[];
  46. int def;
  47.     {
  48.     int best = table[0];
  49.     int i;
  50.  
  51.     if (f == 0)
  52.         return def;
  53.     for (i = 0; i < table[i]; i++)
  54.         if (abs(table[i] - f) < abs(best - f))
  55.             best = table[i];
  56.     return best;
  57.     }    
  58.  
  59.  
  60.  
  61. LOCAL short seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF,
  62.                 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF};
  63.  
  64. LOCAL int search(val, table, size)
  65. int val;
  66. short *table;
  67. int size;
  68.     {
  69.     int        i;
  70.  
  71.     for (i = 0; i < size; i++) 
  72.         {
  73.         if (val <= *table++)
  74.             return i;
  75.         }
  76.     return size;
  77.     }
  78.  
  79. #define    BIAS        (0x84)        /* Bias for linear code. */
  80.  
  81. /*
  82.  * linear2ulaw() - Convert a linear PCM value to u-law
  83.  *
  84.  * In order to simplify the encoding process, the original linear magnitude
  85.  * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
  86.  * (33 - 8191). The result can be seen in the following encoding table:
  87.  *
  88.  *    Biased Linear Input Code    Compressed Code
  89.  *    ------------------------    ---------------
  90.  *    00000001wxyza            000wxyz
  91.  *    0000001wxyzab            001wxyz
  92.  *    000001wxyzabc            010wxyz
  93.  *    00001wxyzabcd            011wxyz
  94.  *    0001wxyzabcde            100wxyz
  95.  *    001wxyzabcdef            101wxyz
  96.  *    01wxyzabcdefg            110wxyz
  97.  *    1wxyzabcdefgh            111wxyz
  98.  *
  99.  * Each biased linear code has a leading 1 which identifies the segment
  100.  * number. The value of the segment number is equal to 7 minus the number
  101.  * of leading 0's. The quantization interval is directly available as the
  102.  * four bits wxyz.  * The trailing bits (a - h) are ignored.
  103.  *
  104.  * Ordinarily the complement of the resulting code word is used for
  105.  * transmission, and so the code word is complemented before it is returned.
  106.  *
  107.  * For further information see John C. Bellamy's Digital Telephony, 1982,
  108.  * John Wiley & Sons, pps 98-111 and 472-476.
  109.  */
  110. LOCAL unsigned char linear2ulaw(pcm_val)
  111. int pcm_val;    /* 2's complement (16-bit range) */
  112.     {
  113.     int        mask;
  114.     int        seg;
  115.     unsigned char    uval;
  116.  
  117.  
  118.     /* Get the sign and the magnitude of the value. */
  119.     if (pcm_val < 0) 
  120.         {
  121.         pcm_val = BIAS - pcm_val;
  122.         mask = 0x7F;
  123.         }
  124.     else 
  125.         {
  126.         pcm_val += BIAS;
  127.         mask = 0xFF;
  128.         }
  129.  
  130.     /* Convert the scaled magnitude to segment number. */
  131.     seg = search(pcm_val, seg_end, 8);
  132.  
  133.     /*
  134.      * Combine the sign, segment, quantization bits;
  135.      * and complement the code word.
  136.      */
  137.     if (seg >= 8)        /* out of range, return maximum value. */
  138.         return 0x7F ^ mask;
  139.     else {
  140.         uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF);
  141.         return uval ^ mask;
  142.     }
  143.  
  144. }
  145.  
  146. LOCAL unsigned int cvt(ch)
  147. int ch;
  148.     {
  149.     return linear2ulaw(ch * 2);
  150.     }
  151.  
  152. #ifdef DEFAULT_SET_MIX
  153. LOCAL int stereo;
  154.  
  155. #ifdef NEW_OUTPUT_SAMPLES_AWARE
  156.  
  157. LOCAL int pps[32], pms[32];
  158.  
  159. void set_mix(percent)
  160. int percent;
  161.     {
  162.     int i;
  163.  
  164.     for (i = 8; i < 32; i++)
  165.         {
  166.         pps[i] = 1 << (31 - i);
  167.         if (i < 29)
  168.             pms[i] = pps[i] - (percent << (29 - i) )/25;
  169.         else
  170.             pms[i] = pps[i] - (percent >> (i - 29) )/25;
  171.         }
  172.     }
  173.  
  174. #else /* NEW_OUTPUT_SAMPLES_AWARE */
  175. /* old code: optimized away */
  176. /* LOCAL int primary, secondary;    */
  177. LOCAL int pps, pms;    /* 1/2 primary+secondary, 1/2 primary-secondary */
  178.  
  179. void set_mix(percent)
  180. int percent;
  181.    {
  182.     percent *= 256;
  183.     percent /= 100;
  184. /*
  185.     secondary = percent;
  186.     primary = 512 - percent;
  187.  */
  188.     pps = 256;
  189.     pms = 256 - percent;
  190.    }
  191. #endif /* NEW_OUTPUT_SAMPLES_AWARE */
  192.  
  193. #endif /* DEFAULT_SET_MIX */
  194.  
  195. #ifdef UNSIGNED_BUFFERS
  196. #define UNSIGNED8
  197. #define UNSIGNED16
  198. #endif
  199.  
  200. #ifdef DEFAULT_BUFFERS
  201.  
  202. #ifdef UNSIGNED16
  203. LOCAL unsigned short *buffer16;
  204. #define VALUE16(x)    ((x)+32768)
  205. #else
  206. LOCAL short *buffer16;
  207. #define VALUE16(x)    (x)
  208. #endif
  209.  
  210. #ifdef UNSIGNED8
  211. LOCAL unsigned char *buffer;
  212. #define VALUE8(x)        ((x)+128)
  213. #else
  214. LOCAL char *buffer;
  215. #define VALUE8(x)        (x)
  216. #endif
  217. LOCAL int idx;
  218. LOCAL int dsize;            /* current data size */
  219. LOCAL int samples_max;    /* number of samples in buffer */
  220.  
  221. LOCAL int tsync = FALSE;
  222. #endif    /* DEFAULT_BUFFERS */
  223.  
  224. #ifdef SEPARATE_BUFFERS
  225. LOCAL char *buffer, *buffer_l, *buffer_r;
  226. LOCAL int idx;
  227. #endif
  228.  
  229.  
  230.  
  231. #ifdef NEW_OUTPUT_SAMPLES_AWARE
  232.  
  233. LOCAL void add_samples16_stereo(left, right, n)
  234. int left, right, n;
  235.     {
  236.     if (pms[n] == pps[n])    /* no mixing */
  237.         {
  238.         if (n<16)
  239.             {
  240.             buffer16[idx++] = VALUE16(left << (16-n) );
  241.             buffer16[idx++] = VALUE16(right << (16-n) );
  242.            }
  243.         else
  244.            {
  245.             buffer16[idx++] = VALUE16(left >> (n-16) );
  246.             buffer16[idx++] = VALUE16(right >> (n-16) );
  247.            }
  248.         }
  249.     else
  250.         {
  251.         int s1, s2;
  252.  
  253.         s1 = (left+right)*pps[n];
  254.         s2 = (left-right)*pms[n];
  255.  
  256.         buffer16[idx++] = VALUE16( (s1 + s2) >> 16);
  257.         buffer16[idx++] = VALUE16( (s1 - s2) >> 16);
  258.         }
  259.     }
  260.  
  261. LOCAL void add_samples16_mono(left, right, n)
  262. int left, right, n;
  263.     {
  264.     if (n<15)        /* is this possible? */
  265.         buffer16[idx++] = VALUE16( (left + right) << (15-n) );
  266.     else
  267.         buffer16[idx++] = VALUE16( (left + right) >> (n-15) );
  268.     }
  269.  
  270. LOCAL void add_samples16(left, right, n)
  271. int left, right, n;
  272.     {
  273.     if (stereo)
  274.         add_samples16_stereo(left, right, n);
  275.     else
  276.         add_samples16_mono(left, right, n);
  277.     }
  278.  
  279. LOCAL void add_samples8_stereo(left, right, n)
  280. int left, right, n;
  281.     {
  282.     if (pms[n] == pps[n])    /* no mixing */
  283.         {
  284.             /* if n<8 -> same problem as above,
  285.                but that won't happen, right? */
  286.         buffer[idx++] = VALUE8(left >> (n-8) );
  287.         buffer[idx++] = VALUE8(right >> (n-8) );
  288.         }
  289.     else
  290.         {
  291.         int s1, s2;
  292.  
  293.         s1 = (left+right)*pps[n];
  294.         s2 = (left-right)*pms[n];
  295.  
  296.         buffer[idx++] = VALUE8( (s1 + s2) >> 24);
  297.         buffer[idx++] = VALUE8( (s1 - s2) >> 24);
  298.         }
  299.     }
  300.  
  301. LOCAL void add_samples8_mono(left, right, n)
  302. int left, right, n;
  303.     {
  304.     buffer[idx++] = VALUE8( (left+right) >> (n-7) );
  305.     }
  306.  
  307. LOCAL void add_samples8(left, right, n)
  308. int left, right, n;
  309.     {
  310.     if (stereo)
  311.         add_samples8_stereo(left, right, n);
  312.     else
  313.         add_samples8_mono(left, right, n);
  314.     }
  315.  
  316. #else
  317.  
  318. /* don't ask me if this code is correct then (I guess it is) ...
  319.    anyone still using it? */
  320. LOCAL void add_samples16_stereo(left, right)
  321. int left, right;
  322.     {
  323.     if (pms == pps)    /* no mixing */
  324.         {
  325.         buffer16[idx++] = VALUE16(left/256);
  326.         buffer16[idx++] = VALUE16(right/256);
  327.         }
  328.     else
  329.         {
  330.         int s1, s2;
  331.  
  332.         s1 = (left+right)*pps;
  333.         s2 = (left-right)*pms;
  334.  
  335.         buffer16[idx++] = VALUE16( (s1 + s2)/65536 );
  336.         buffer16[idx++] = VALUE16( (s1 - s2)/65536 );
  337.         }
  338.     }
  339.  
  340. LOCAL void add_samples16_mono(left, right)
  341. int left, right;
  342.     {
  343.     buffer16[idx++] = VALUE16( (left + right)/256);
  344.     }
  345.  
  346. LOCAL void add_samples16(left, right)
  347. int left, right;
  348.     {
  349.     if (stereo)
  350.         add_samples16_stereo(left, right);
  351.     else
  352.         add_samples16_mono(left, right);
  353.     }
  354.  
  355. LOCAL void add_samples8_stereo(left, right)
  356. int left, right;
  357.     {
  358.     if (pms == pps)    /* no mixing */
  359.         {
  360.         buffer[idx++] = VALUE8(left/65536);
  361.         buffer[idx++] = VALUE8(right/65536);
  362.         }
  363.     else
  364.         {
  365.         int s1, s2;
  366.  
  367.         s1 = (left+right)*pps;
  368.         s2 = (left-right)*pms;
  369.  
  370.         buffer[idx++] = VALUE8( (s1 + s2) >> 24);
  371.         buffer[idx++] = VALUE8( (s1 + s2) >> 24);
  372.         }
  373.     }
  374.  
  375. LOCAL void add_samples8_mono(left, right)
  376. int left, right;
  377.     {
  378.     buffer[idx++] = VALUE8( (left+right) >> 16);
  379.     }
  380.  
  381. LOCAL void add_samples8(left, right)
  382. int left, right;
  383.     {
  384.     if (stereo)
  385.         add_samples8_stereo(left, right);
  386.     else
  387.         add_samples8_mono(left, right);
  388.     }
  389.  
  390. #endif
  391.  
  392. #ifndef NEW_OUTPUT_SAMPLES_AWARE
  393.  
  394. XT void output_samples P((int left, int right, int n));
  395.  
  396. void output_samples(left, right, n)
  397. int left, right, n;
  398.     {
  399.     old_output_samples(left >> (n-23), right >> (n-23));
  400.     }
  401. #define output_samples    old_output_samples
  402.  
  403. #endif
  404.  
  405. #ifndef NEW_FUNCS
  406. void sync_audio(function, parameter)
  407. void (*function) P((void *));
  408. GENERIC parameter;
  409.     {
  410.     (*function)(parameter);
  411.     }
  412.  
  413. void audio_ui(c)
  414. int c;
  415.     {
  416.     }
  417.  
  418. #endif
  419.